package com.panshi.config;

import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class MyCacheConfig {
    @Bean
    public RedisTemplate<String, ?> redisCacheTemplate(RedisConnectionFactory redisConnectionFactory) {
        //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值（默认使用JDK的序列化方式）
        //使用Fast对象，存储对象的时候转换成json格式存储，反向查询的时候返回也是json格式，再转换成对象
        RedisSerializer<?> valSeri=new GenericFastJsonRedisSerializer();//<>(Object.class);
        //字符串作为key StringRedisSerializer
        RedisSerializer<String> keySeri=new StringRedisSerializer();

        RedisTemplate<String, ?> template = new RedisTemplate<>();

        template.setKeySerializer(keySeri);
        template.setValueSerializer(valSeri);

        template.setHashKeySerializer(keySeri);
        template.setHashValueSerializer(valSeri);
        template.setStringSerializer(keySeri);

        template.setConnectionFactory(redisConnectionFactory);

        return template;
    }

    //@EnableCaching 配置管理对象
    @Bean
    public RedisCacheConfiguration redisCacheConfiguration() {
        //只有fastjson稳定
        RedisSerializer<?> valSeri=new GenericFastJsonRedisSerializer();
        return RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(valSeri));
    }

}
